﻿using System;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;

namespace Assets.Scripts.UI
{
    public enum PanelState
    {
        Enter,
        Pause,
        Resume,
        Exit,
    }
    public class BasePanel : MonoBehaviour
    {
        private PanelState panelState;
        protected Button closeBtn;
        protected CanvasGroup canvasGroup;
        private void Awake()
        {
            closeBtn = FindBtnByName("CloseBtn");
            canvasGroup = GetComponent<CanvasGroup>();
            if (closeBtn != null)
            {
                closeBtn.onClick.AddListener(UIManager.Instance.PopPanel);
            }
            if (canvasGroup == null)
            {
                canvasGroup = gameObject.AddComponent<CanvasGroup>();
            }
        }
        public virtual void OnEnter()
        {
            canvasGroup.alpha = 1;
            canvasGroup.blocksRaycasts = true;
            panelState = PanelState.Enter;
        }
        public virtual void OnPause()
        {
            canvasGroup.alpha = 1;
            canvasGroup.blocksRaycasts = false;
            panelState = PanelState.Pause;
        }
        public virtual void OnResume()
        {
            canvasGroup.alpha = 1;
            canvasGroup.blocksRaycasts = true;
            panelState = PanelState.Resume;
        }
        public virtual void OnExit()
        {
            canvasGroup.alpha = 0;
            canvasGroup.blocksRaycasts = false;
            panelState = PanelState.Exit;
        }
        public Button FindBtnByName(string childName)
        {
            Button closeBtn = null;
            foreach (var item in GetComponentsInChildren<Button>())
            {
                if (item.name == childName)
                {
                    closeBtn = item;
                }
            }
            return closeBtn;
        }
        public PanelState GetPanelState()
        {
            return panelState;
        }
        public UIPanelType GetPanelType()
        {
            return Enum.Parse<UIPanelType>(name.Replace("(Clone)",""));
        }
    }
}